home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / satellit / pacdoc / unixtime.c < prev    next >
C/C++ Source or Header  |  1993-04-15  |  3KB  |  77 lines

  1. /* These routines are used instead of the standard library time routines to force
  2. ** correct handling of "timestamps" in "UNIX time", instead of "ANSI time".
  3. ** The library routines time() gmtime() and mktime() are either ANSI or UNIX.
  4. ** The routine initJwtime attempts to find out which by passing in a UNIX time
  5. ** for 1/1/92 and finding out if the year coming from gmtime() is 1992. If the
  6. ** year is 1992, then we know the library is UNIX. Otherwise, we know it is ANSI.
  7. ** The constant offset variable "ansi_offset" is then set accordingly, either to
  8. ** 0 for UNIX libraries, or to the ANSI timestamp of 01/01/70 if the library is
  9. ** ANSI. This offset is then used to adjust serial numbers going into and coming
  10. ** out of the library routines.
  11. **
  12. ** The only problem is how to get a program written with calls to gmtime() time()
  13. ** and mktime() to use the routines unix_gmtime() unix_time() and unix_mktime().
  14. ** This is achieved in the file unixtime.h, which is included in the program
  15. ** instead of time.h.
  16. */
  17. #include <time.h>
  18. #define T_010170 0x83abd000 /* count from ANSI time base - 0101 1900 to 01/01/70 */
  19. #define T_010192 0x29610480 /* count from 01/01/70 to 01/01/92 */
  20. int jwtimeInited = 0;       /* Set when inited */
  21. unsigned long ansi_offset;  /* Set 0 if time(), gmtime() and mktime() are 01/01/70     */
  22.                             /* based. Otherwise set to offset of 01/01/70 from ansi */
  23.                             /* time base of 01/01 1900                                 */
  24.  
  25. void initJwtime(void){
  26.     time_t t;
  27.     t = T_010192;
  28.     if (gmtime(&t)->tm_year != 92)
  29.         ansi_offset = T_010170;
  30.     else
  31.         ansi_offset = 0l;
  32.     jwtimeInited = 1;
  33. }
  34.  
  35. time_t unix_time(time_t * arg){
  36.     if (! jwtimeInited)
  37.         initJwtime();
  38.  
  39.      if (arg != NULL)
  40.          *arg = time(NULL) - ansi_offset;
  41.  
  42.     return time(NULL)-ansi_offset;
  43. }
  44.  
  45. struct tm * unix_gmtime(time_t * arg){
  46.     time_t t;
  47.  
  48.     if (! jwtimeInited)
  49.         initJwtime();
  50.  
  51.     t = *arg + ansi_offset;
  52.     return gmtime(&t);
  53. }
  54.  
  55. time_t unix_mktime(struct tm * arg){
  56.     time_t t;
  57.  
  58.     if (! jwtimeInited)
  59.         initJwtime();
  60.  
  61.     return (mktime(arg) - ansi_offset);
  62. }
  63.  
  64. /* Convert a 1970 epoc second count to a 1900 epoc day count */
  65. /* Use these values in EXCEL and other microsoft products.     */
  66. /* Note that although day 1 in the time() world is the second */
  67. /* day, day 1.00 in the Microsoft floating point world is the */
  68. /* first day, Jan 1 1900. So 1.5 is Noon on Jan 1 1900.             */
  69. #define DATEVAL010170 25569.0
  70. double TimeVal(time_t t){
  71.     double temp;
  72.     temp = (double) t;
  73.     temp /= (24.0 * 60.0 * 60.0);
  74.     temp += DATEVAL010170;
  75.     return temp;
  76. }
  77.